﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Marbles.Core.Connectors;

namespace Marbles.Core.Content.Profiles
{
    public class ProfileResponse
    {
        public ProfileQuestion Question { get { return _question; } }
        public string Response { get { return _response; } set { _response = value; } }
        public string UserId { get { return _userid; } }

        private string _response = string.Empty;
        private ProfileQuestion _question = null;
        private string _userid = string.Empty;

        public ProfileResponse()
        {
            _userid = "";
            _question = null;
            _response = "";
        }

        public ProfileResponse(string UserId, ProfileQuestion Question)
            : this()
        {
            _userid = UserId;
            _question = Question;
            _response = "";
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT response FROM userProfiles WHERE siteid = @siteid AND userid = @userid AND questionid = @questionid");
            cmd.AddParameter("@siteid", Question.SiteId);
            cmd.AddParameter("@questionid", Question.QuestionId);
            cmd.AddParameter("@userid", _userid);
            DataTable dt = dbc.FetchDataTable(cmd);
            if (dt.Rows.Count > 0) { 
                _response = dt.Rows[0]["response"].ToString().Trim();
                if (Question.QuestionType == ProfileQuestionType.ImageUpload)
                {
                    _response = string.Format("/App_Data/Profile/{0}/{1}", UserId, dt.Rows[0]["response"].ToString().Trim());
                }
            }
        }

        internal ProfileResponse(DataRow row)
        {
            _question = new ProfileQuestion(Convert.ToInt32(row["siteid"]), Convert.ToInt32(row["questionid"]));
            _response = row["response"].ToString();
            _userid = row["userid"].ToString();
        }

        public void Delete()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("DELETE FROM userProfiles WHERE siteid = @siteid AND questionid = @questionid AND userid = @userid");
            cmd.AddParameter("@siteid", Question.SiteId);
            cmd.AddParameter("@questionid", Question.QuestionId);
            cmd.AddParameter("@userid", _userid);
            dbc.ExecuteCommand(cmd);
        }

        public void Insert()
        {
            if (Exists()) { Update(); return; }
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("INSERT INTO userProfiles (siteid, questionid, userid, response) VALUES (@siteid, @questionid, @userid, @response)");
            cmd.AddParameter("@siteid", Question.SiteId);
            cmd.AddParameter("@questionid", Question.QuestionId);
            cmd.AddParameter("@userid", _userid);
            cmd.AddParameter("@response", _response.Trim());
            dbc.ExecuteCommand(cmd);
        }

        public void Update()
        {
            if (!Exists()) { Insert(); return; }
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("UPDATE userProfiles SET response = @response WHERE siteid = @siteid AND questionid = @questionid AND userid = @userid");
            cmd.AddParameter("@siteid", Question.SiteId);
            cmd.AddParameter("@questionid", Question.QuestionId);
            cmd.AddParameter("@userid", _userid);
            cmd.AddParameter("@response", _response.Trim());
            dbc.ExecuteCommand(cmd);
        }

        public bool Exists()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT userid FROM userProfiles WHERE siteid = @siteid AND questionid = @questionid AND userid = @userid");
            cmd.AddParameter("@siteid", Question.SiteId);
            cmd.AddParameter("@questionid", Question.QuestionId);
            cmd.AddParameter("@userid", _userid);
            DataTable dt = dbc.FetchDataTable(cmd);
            return dt.Rows.Count > 0;
        }

        public static ProfileResponse CreateNew(ProfileQuestion Question, string UserId)
        {
            ProfileResponse item = new ProfileResponse();
            item._question = Question;
            item._userid = UserId;
            return item;
        }
    }
}